/* * 
 * Copyright © 2008 HNA Co. Ltd. 
 * All right reserved. 
 */

package com.gamutsoft.amb.common.util;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.extremecomponents.table.core.TableConstants;
import org.extremecomponents.table.limit.Filter;
import org.extremecomponents.table.limit.FilterSet;
import org.extremecomponents.table.limit.Limit;
import org.extremecomponents.table.limit.Sort;

import com.gamutsoft.amb.common.bean.CondictionsBean;



/**
 * <h3>Class name</h3>
 * ecTable的分页类
 * <h4>Description</h4>
 * <h4>Special Notes</h4>
 * 
 * @ver 0.1
 * @author Jay.Wu 2008-8-26
 */
public class EcPageUtil extends PageUtil {

	public static int pageSize = 20;

	public Limit limit;

	public EcPageUtil(HttpServletRequest request) {
		super(request);
		initPageUtill(request);
	}

	public void initPageUtill(HttpServletRequest request) {
		limit = ExtremeTablePage.getLimit(request);
		this.setPageSize(limit.getCurrentRowsDisplayed());
		this.setStrPage(String.valueOf(limit.getPage()));
	}

	public void setPages() {
		super.setPages();
		request.setAttribute(TableConstants.TOTAL_ROWS, getTotalRecord());
	}

	/**
	 * 将ec的过滤器查询语句
	 * 
	 * @return
	 */
	public List<CondictionsBean> getCondictions() {
		List<CondictionsBean> list = null;

		FilterSet filterSet = limit.getFilterSet();
		if (filterSet.isFiltered() && !filterSet.isCleared()) {
			Filter[] filters = filterSet.getFilters();

			if (filters != null && filters.length > 0) {
				list = new ArrayList<CondictionsBean>();
				for (int i = 0; i < filters.length; i++) {
					CondictionsBean condictionsBean = new CondictionsBean();
					condictionsBean.setProperty(filters[i].getProperty());
					condictionsBean.setCondiction("like");
					condictionsBean.setValue("%" + filters[i].getValue() + "%");
					list.add(condictionsBean);
				}
			}
		}

		return list;
	}

	/**
	 * 将ec的排序查询语句
	 * 
	 * @return
	 */
	public String getSorts() {
		String order = null;
		Sort sort = limit.getSort();

		if (sort != null && sort.isSorted()) {
			order = " order by " + sort.getProperty() + " "
					+ sort.getSortOrder() + " ";
		}

		return order;
	}
}
